{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2695cb9b",
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import yfinance as yf\n",
    "from utils import plot_returns, print_metrics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b0e4c77",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Download front-month futures data of S&P500, 10-year Treasuries, gold and US dollar\n",
    "symbols = ['ES=F', 'ZN=F', 'GC=F', 'DX=F']\n",
    "data = yf.download(symbols)\n",
    "# Resample data so that we deal with monthly data instead of daily to reduce noise\n",
    "data = data.resample(\"M\").last()\n",
    "data.index = pd.to_datetime(data.index)\n",
    "# Subset adjusted close prices and fill NaNs with value know at time t\n",
    "# Drop rows with unknown prices in the beginning of the dataset\n",
    "prices = data[\"Adj Close\"].ffill().dropna()\n",
    "prices.index = pd.to_datetime(prices.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b0e2b92",
   "metadata": {
    "lines_to_next_cell": 1
   },
   "outputs": [],
   "source": [
    "# Compute logarithmic returns\n",
    "log_returns = np.log(prices).diff()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9385da16",
   "metadata": {
    "lines_to_next_cell": 1
   },
   "outputs": [],
   "source": [
    "def compute_risk_parity_weights(returns, window_size=36):\n",
    "    # compute volatility known at time t\n",
    "    rolling_vol = None\n",
    "    rolling_inverse_vol = None\n",
    "    # divide inverse volatility by the sum of inverse volatilities\n",
    "    risk_parity_weights = None\n",
    "    return risk_parity_weights"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "90b703c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Compute risk parity weights\n",
    "risk_parity_weights = compute_risk_parity_weights(log_returns)\n",
    "# shift weights by one period to use only information available at time t\n",
    "risk_parity_weights = None\n",
    "weighted_returns = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07b59a5a",
   "metadata": {},
   "outputs": [],
   "source": [
    "print_metrics(weighted_returns)\n",
    "plot_returns(weighted_returns)"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
